<?php
include("../UserIncludes/DbConfig.php");
$mdid=$_REQUEST['id'];
$mdid='093f65e080a295f8076b1c5722a46aa2';
$mdid='32bb90e8976aab5298d5da10fe66f21d';
$mdid='d2ddea18f00665ce8623e36bd4e3c7c5';
$mdid='c4ca4238a0b923820dcc509a6f75849b'; // 1
$nets=array();
$ads=array();

$SQL_NET="SELECT  N.`networkId`,M.`adsiteId`, D.`allowNetwork` FROM  `adspace_master` M, `adspace_details` D, `network_sites` N, `network_master` NM WHERE M.`adspaceId`=D.`adspaceId` AND D.`adspaceIdMd`='".$mdid."' AND N.`siteId`=M.`adsiteId` AND NM.`networkId`=N.`networkId` AND NM.`networkType`=D.`adspaceType`";
$RS_NET=mysql_query($SQL_NET) or die("Error !! #1=>".mysql_error());
while($ROW_NET=mysql_fetch_array($RS_NET)){
$nets[]=$ROW_NET[networkId];
}
echo "<pre>";
print_r($nets);
echo "</pre>";
if(count($nets)){
foreach($nets as $net){
$SQL.=" SELECT AD.*,SD.*,AM.* FROM advertise_details AD, adspace_details SD,advertise_master AM";
//$SQL.=" WHERE AD.adspaceId=SD.adspaceId AND AD.advertiseId=AM.advertiseId AND SD.`allowNetwork`='Y'AND SD.adspaceIdMd='".$mdid."' ";
$SQL.=" WHERE AD.advertiseId=AM.advertiseId AND SD.`allowNetwork`='Y' AND SD.adspaceIdMd='".$mdid."' ";
$SQL.=" AND AM.startDate <= CURDATE() and AM.endDate >= CURDATE()";
$SQL.=" AND AM.advertiseStatus = 'Y' ";
$SQL.=" AND AD.networkId =$net";
$SQL.=" UNION ";
}
}

//echo $SQL."<br>";


#Direct Ads
//$SQL="SELECT AD.adDetailasId,AD.advertiseId FROM advertise_details AD, adspace_details SD,advertise_master AM";
$SQL.=" SELECT AD.*,SD.*,AM.* FROM advertise_details AD, adspace_details SD,advertise_master AM";
$SQL.=" WHERE AD.adspaceId=SD.adspaceId AND AD.advertiseId=AM.advertiseId AND SD.adspaceIdMd='".$mdid."' ";
$SQL.=" AND AM.startDate <= CURDATE() and AM.endDate >= CURDATE()";
$SQL.=" AND AM.advertiseStatus = 'Y' ";
$SQL.=" AND AD.networkId = 0 ";

$RS=mysql_query($SQL) or die("Error !! #2=>".mysql_error());
$ctr=0;
while($ROW=mysql_fetch_array($RS)){
$ctr++;
$ads[]=$ROW[adDetailasId];
// }
// echo "Ad Size". $ROW['adSize'];
// if(count($ads)){$SQL="SELECT * FROM advertise_details WHERE adDetailasId IN (".implode(",", $ads).")";
// $RS=mysql_query($SQL) or die("Error !! #2 ");
// while($ROW=mysql_fetch_array($RS)){
// $ads[]=$ROW[adDetailasId];
// //}

if($ROW[adSize]=="184x360" && $ROW[thumbOrientation]=="vertical" ){
$val_tr=1;
}elseif($ROW[adSize]=="468x60" && $ROW[thumbOrientation]=="horizontal" ){
$val_tr=2;
}elseif($ROW[adSize]=="234x60" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="728x90" && $ROW[thumbOrientation]=="horizontal"){
$val_tr=5;
}elseif($ROW[adSize]=="125x125" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="120x240" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="180x150" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="300x250" && $ROW[thumbOrientation]=="vertical"){
$val_tr=2;
}elseif($ROW[adSize]=="336x280"  && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="250x250"  && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="120x600" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="160x600" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="" && $ROW[thumbOrientation]=="vertical"){
$val_tr=1;
}elseif($ROW[adSize]=="" && $ROW[thumbOrientation]=="horizontal"){ 
$val_tr=$ROW[noOfTextAd];
}else{
$val_tr=2;
}
		$td++;
		  $tr++;
				if($tr =="$val_tr"){
				   $tr = 0;
				 }
if($ROW[fontType]!=""){
$fontType=$ROW[fontType];
}else{
$fontType="Arial";
}
if($ROW[backgroundColor]!=""){
$backgroundColor=$ROW[backgroundColor];
}else{
$backgroundColor="#E8E8E8";
}
//echo $backgroundColor;
if($ROW[headerColor]!=""){
$headerColor=$ROW[headerColor];
}else{
$headerColor="#E8E8E8";
}
//echo $headerColor;
if($ROW[headerColor]!=""){
$bodyColor=$ROW[bodyColor];
}else{
$bodyColor="#000000";
}
//echo $bodyColor;
?>
<td align="left"><a href="http://esolz.org/admobweb_new/Delivery/aClick_frame.php?mm=<?php echo $ROW[advertiseId];?>" target="ifrm_<?php echo $ROW[advertiseId];?>">
		<table width="250" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $backgroundColor;?>" onClick="javascript:_aClick(<?php echo $ROW[advertiseId];?>,'<?php echo $_SERVER["REMOTE_ADDR"];?>');">
          <tr>
             <td align="center"><?php echo $ctr; ?><a href="<?php echo $$ROW[destinationUrl];?>" style="font-family: <?php echo $fontType;?>; color:<?php echo $headerColor;?>;"><?php echo $ROW[adTitle];	?> </a></td>
           <!--</tr>
			     <tr>-->
             <td style="padding-left:20px;"><font color="<?php echo $bodyColor;?>" style="font-family: <?php echo $fontType;?>;"><?php echo $ROW[adTitle]; ?></font></td>
          </tr>
      </table></a>
  <iframe width="100" height="50" name="ifrm_<?php echo $ROW[advertiseId];?>" id="ifrm_<?php echo $ROW[advertiseId];?>" src="" frameborder="1">Your browser doesn't support iframes.</iframe>  
		<?php
echo "</td>";
		if($td=="$val_tr"){
    	echo '</tr><tr>';
								$td=0;
		 }
} // End of While
//}
sort($ads);
echo "<pre>";
print_r($ads);
echo "</pre>";
echo $SQL;
// Insert into the ads array
// 
// 
// #Networ Ads

$SQL_NET="SELECT  N.`networkId`,M.`adsiteId`, D.`allowNetwork` FROM  `adspace_master` M, `adspace_details` D, `network_sites` N WHERE M.`adspaceId`=D.`adspaceId` AND D.`adspaceIdMd`='".$mdid."' AND N.`siteId`=M.`adsiteId` ";
//echo "<br>".$SQL_NET;

$SQL="SELECT * FROM advertise_details AD, adspace_details SD,advertise_master AM";
$SQL.=" WHERE AD.adspaceId=SD.adspaceId AND AD.advertiseId=AM.advertiseId AND SD.`allowNetwork`='Y'";
$SQL.=" AND AM.startDate <= CURDATE() and AM.endDate >= CURDATE()";
$SQL.=" AND AM.advertiseStatus = 'Y' ";
$SQL.=" AND AD.networkId =7 ";

//echo "<br>".$SQL;
// 
// $sql_advertise="select * from advertise_details AD,advertise_master AM where AD.advertiseId=AM.advertiseId AND AD.networkId != 0 and AM.advertiseStatus = 'Y' ";
// 
// //$sites = mysql_query($sql_advertise);
// $sites = mysql_query($SQL_NET);
// $arr = array();
// while($row_sites = mysql_fetch_array($sites))
// {
// 
// $query_site = "select * from ";
// echo "Net Id--->".$row_sites['networkId']."<br><br>";
// 
// $sql_advertise_inside="select ASM.adsiteId,NAC.categoryId from adsite_master ASM,network_ad_category NAC where NAC.networkId='$row_sites[networkId]' AND NAC.categoryId =  ASM.adcategoryId ";
// $any = mysql_query($sql_advertise_inside);
// while($res_site = mysql_fetch_array($any)){
// if(!in_array($res_site[adsiteId],$arr)){
// $arr[] = $res_site[adsiteId];
// }
// }
// }
// echo "<pre>";
// print_r($arr);
// echo "</pre>";

// 
// Insert into the ads array if not in array
// 
// $adSpaceid
//   check $allownetWork
// $adSiteId
// 
// $netWorkId
// 
// for each $netWorkId 
//    Find $advertiseId where $adDisplayType=$adspaceType
//    
//    $StartDate    $endDate   $IP2Country $tags  $budget   $evenly_over_the_day
// 
##############  

 // Count of clicks

##################
# Custom Network
# 
# Match netWorkId for direct network, and categories for custom network
# 
# Following Tables are used
# 
# Create_network
# adcampaign
# adgroup
# adgroup_ad
# 








function IP2Country($ipAddr)
{
// 72.167.131.121
// 71.6.131.6 
// 82.244.68.106
$location = $ipAddr;
$q = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&location='.rawurlencode($location);

// $ch = curl_init($q);
// curl_setopt($ch, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
// curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($q)); // use HTTP POST to send form data
// $resp = curl_exec($ch); //execute post and get results
// curl_close ($ch);

$resp=file_get_contents($q);
$xml = simplexml_load_string($resp);
//echo "country -----".$xml->Result[0]->Country;
//echo "<br>State----".$xml->Result[0]->State;/
//echo "<br>City----".$xml->Result[0]->City;
return $xml;
}
// $IP='82.244.68.106';
// $IP='72.167.131.121';
// $IP='71.6.131.6';
// echo "<pre>";
// print_r(IP2Country($IP));
// echo "</pre>";

function debug($name, $data){
	ob_start();
	print_r($data);
	$str = ob_get_contents();
	ob_end_clean();
	debug_var($name, $str);
}

function debug_var($name, $data){
	$captured = preg_split("/\r?\n/", $data);

	print "<script>function toggleDiv(num){var span = document.getElementById('d'+num);var a = document.getElementById('a'+num);var cur = span.style.display;if(cur == 'none'){a.innerHTML = '-';span.style.display = 'inline';}else{a.innerHTML = '+';span.style.display = 'none';}}</script>";
	print "<b>$name</b>\n";
	print "<pre>\n";

	foreach($captured as $line){ print debug_colorize_string($line) . "\n"; }

	print "</pre>\n";
}
// 
//  var m3_r = Math.floor(Math.random()*99999999999);
//    if (!document.MAX_used) document.MAX_used = ',';
//    document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
//    document.write ('&amp;cb=' + m3_r);
//    if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
//    document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
//    document.write ("&amp;loc=" + escape(window.location));
//    if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
//    if (document.context) document.write ("&context=" + escape(document.context));
//    if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
//    document.write ("'><\/scr"+"ipt>");
// //]]>--></script><noscript><a href='http://esolz.org/openx280/www/delivery/ck.php?n=a216ae51&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://esolz.org/openx280/www/delivery/avw.php?cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a216ae51' border='0' alt='' /></a></noscript>

?>
<script type='text/javascript'>
function _aClick(adv,clt)
{
var hst=window.location.hostname;
//var hst="http://" + window.location.toString().split("//")[1].split("/")[0] + "/";
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
alert(adv);
alert(hst);
document.ifrm_+adv+.src="http://www.google.com";
//alert(clt);
//  xmlHttp.open("GET","http://esolz.org/admobweb_new/Delivery/aClick.php?adv="+adv+"&clt="+clt+"&hst="+hst+"",true);
 // xmlHttp.send(null);
}
</script>

This <a href="http://www.google.com" target="ifrm">link</a>
<iframe width="100" height="50" name="ifrm" id="ifrm" src="" frameborder="1">Your browser doesn't support iframes.</iframe>
